﻿namespace SmartSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;using NLog;

    public class DmVtDataAccess
    {
        private static Logger log = LogManager.GetCurrentClassLogger();
        public DataSet GetDataSet()
        {
            DataSet set2;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmVt_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");                
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception);
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataSet GetDataSet(string ps_Code)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Vt", ps_Code) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmVt_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");                
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public DataSet GetDataSetByParent(string ps_ParentCode)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Nh_Vt", ps_ParentCode) };
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            DataSet dataSet = new DataSet("VBSP");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmVt_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet, "ITC");                
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return set2;
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_Vt", row["Ma_Vt"]), new SqlParameter("p_Ten_Vt", row["Ten_Vt"]), new SqlParameter("p_Ten_Vt_E", row["Ten_Vt_E"]), new SqlParameter("p_Dvt", row["Dvt"]), new SqlParameter("p_Dvt_E", row["Dvt_E"]), new SqlParameter("p_Gia_Mua", row["Gia_Mua"]), new SqlParameter("p_Gia_Ban", row["Gia_Ban"]), new SqlParameter("p_Dvt0", row["Dvt0"]), new SqlParameter("p_He_So0", row["He_So0"]), new SqlParameter("p_Gia_Mua0", row["Gia_Mua0"]), new SqlParameter("p_Gia_Ban0", row["Gia_Ban0"]), new SqlParameter("p_Dvt1", row["Dvt1"]), new SqlParameter("p_He_So1", row["He_So1"]), new SqlParameter("p_Gia_Mua1", row["Gia_Mua1"]), 
                new SqlParameter("p_Gia_Ban1", row["Gia_Ban1"]), new SqlParameter("p_Dvt2", row["Dvt2"]), new SqlParameter("p_He_So2", row["He_So2"]), new SqlParameter("p_Gia_Mua2", row["Gia_Mua2"]), new SqlParameter("p_Gia_Ban2", row["Gia_Ban2"]), new SqlParameter("p_Dvt3", row["Dvt3"]), new SqlParameter("p_He_So3", row["He_So3"]), new SqlParameter("p_Gia_Mua3", row["Gia_Mua3"]), new SqlParameter("p_Gia_Ban3", row["Gia_Ban3"]), new SqlParameter("p_Loai_Vt", row["Loai_Vt"]), new SqlParameter("p_Ma_Nh_Vt", row["Ma_Nh_Vt"]), new SqlParameter("p_Stt_Nh_Vt", row["Stt_Nh_Vt"]), new SqlParameter("p_SL_Ton_Min", row["SL_Ton_Min"]), new SqlParameter("p_SL_Ton_Max", row["SL_Ton_Max"]), new SqlParameter("p_Tk_Vt", row["Tk_Vt"]), new SqlParameter("p_Tk_Gv", row["Tk_Gv"]), 
                new SqlParameter("p_Tk_Dt", row["Tk_Dt"]), new SqlParameter("p_Tk_Ck", row["Tk_Ck"]), new SqlParameter("p_Tk_HBTL", row["Tk_HBTL"]), new SqlParameter("p_Ma_Sp", row["Ma_Sp"]), new SqlParameter("p_Ma_Nh_Dm", row["Ma_Nh_Dm"]), new SqlParameter("p_Is_Stop", row["Is_Stop"]), new SqlParameter("p_Thue_Xuat", row["Thue_Xuat"]), new SqlParameter("p_Ma_Loai1", row["Ma_Loai1"]), new SqlParameter("p_Ma_Loai2", row["Ma_Loai2"]), new SqlParameter("p_Ma_Loai3", row["Ma_Loai3"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[1].Direction = ParameterDirection.InputOutput;
            values[1].SqlDbType = SqlDbType.BigInt;
            values[0x2a].Direction = ParameterDirection.Output;
            values[0x2a].SqlDbType = SqlDbType.Int;
            SqlConnection SSConnection = SSDataAccess.SSConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmVt_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = SSConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();                
                row["Stt"] = values[1].Value;
                num = (int) values[0x2a].Value;
            }
            catch (Exception exception)
            {
                log.ErrorException(string.Empty, exception); 
                throw new NotSupportedException("Invalid expression", exception);
            }
            finally
            {
                SSDataAccess.Close(SSConnection);
            }
            return num;
        }
    }
}

